package com.app.controller;


import com.app.common.EducationTool;
import com.app.common.ExperienceTool;
import com.app.common.ScaleTool;
import com.app.model.*;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@RestController
@Controller
@NoArgsConstructor
@AllArgsConstructor
@Data
public class JobFairCompanyPostController {


    @Autowired
    UserService userService;

    @Autowired
    ComRecruitersService comRecruitersService;

    @Autowired
    JobFairCompanyService jobFairCompanyService;
    @Autowired
    JobFairCompanyPostService jobFairCompanyPostService;
    @Autowired
    EnterpriseService enterpriseService;
    @Autowired
    JobPublishService jobPublishService;
    @Autowired
    PositionService positionService;
    @Autowired
    CustomerService customerService;
    @Autowired
    LookMeService lookMeService;
    @Autowired
    EnterpriseAlbumService enterpriseAlbumService;

    @Autowired
    IndustryService industryService;


    //企业参加招聘会
    @PostMapping("/insertFairCompany")
    public Result<Object> insertFairCompany(
            @RequestParam("postIds") String postIds,
            @RequestParam("sId") Integer sId,
            HttpServletRequest request) throws ParseException {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");

        }
        String mobile = TokenUtils.verify(token);
        UserModel user = userService.getUserByMobile(mobile);

        if (user == null) {
            return Result.error(405, "用户不存在");

        }
        ComRecruitersModel com = comRecruitersService.getComRecruitersById(user.getRId());
        //添加企业记录
        JobFairCompanyModel model = new JobFairCompanyModel();
        model.setCid(com.getComId());
        model.setSid(sId);
        model.setCrtTime(String.valueOf(CommonHelp.getNowTimestamp()));
        JobFairCompanyModel jobFairCompany = jobFairCompanyService.getModelBySidAndCid(sId, com.getComId());
        if (jobFairCompany == null) {
            jobFairCompanyService.insert(model);
            //添加岗位记录
            String str[] = postIds.split(",");
            for (int i = 0; i < str.length; i++) {
                JobFairCompanyPostModel jobFairCompanyPost = new JobFairCompanyPostModel();
                jobFairCompanyPost.setCid(com.getComId());
                jobFairCompanyPost.setSid(sId);
                jobFairCompanyPost.setPid(Integer.parseInt(str[i]));
                jobFairCompanyPostService.insert(jobFairCompanyPost);
            }
        } else {
            return Result.success("已预订,请勿重复预定");
        }

        return Result.success("预定成功");
    }

    //查询参会企业
    @PostMapping("/getEnterpriseByFair")
    public Result<Object> getEnterpriseByFair(
            @RequestParam("sId") Integer sId,
            HttpServletRequest request) {

        return Result.success(jobFairCompanyService.getEnterpriseByFair(sId));
    }

    //查询参会岗位
    @GetMapping("/getPublishByFair")
    public Result<Object> getPublishByFair(
            @RequestParam("sId") Integer sId,
            HttpServletRequest request) {

        return Result.success(jobFairCompanyService.getPublishByFair(sId));
    }


    //查询参会企业详情
    @PostMapping("/getEnterpriseDetails")
    public Result<Object> getEnterpriseDetails(
            @RequestParam("sId") Integer sId,
            @RequestParam("cId") Integer cId,
            HttpServletRequest request) throws ParseException {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");

        }
        String mobile = TokenUtils.verify(token);
        UserModel user = userService.getUserByMobile(mobile);

        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        CustomerModel cus = customerService.getCustomerById(user.getCId());
        EnterpriseModel enterprise;
        enterprise = enterpriseService.findModelById(cId);
        if (enterprise == null) {
            return Result.success(null);
        }
        if (enterprise.getWelfare() != null && enterprise.getWelfare() != "") {
            String[] str = enterprise.getWelfare().split(",");
            List<String> welfareList = new ArrayList<>();
            for (int j = 0; j < str.length; j++) {
                welfareList.add(str[j]);
            }
            enterprise.setWelfareList(welfareList);
        } else {
            enterprise.setWelfareList(null);
        }
        List<EnterpriseAlbumModel> enterpriseAlbumModel = enterpriseAlbumService.getListByComId(enterprise.getId());
        enterprise.setAlbumModelList(enterpriseAlbumModel);
        for (int j = 0; j < ScaleTool.SCALE_VO_LIST.size(); j++) {
            if (enterprise.getScaleId() == ScaleTool.SCALE_VO_LIST.get(j).getId()) {
                enterprise.setScaleName(ScaleTool.SCALE_VO_LIST.get(j).getData());
            }
        }
        IndustryModel industryModel = industryService.getModelByCode(enterprise.getEnterpriseType());
        enterprise.setEnterpriseTypeText(industryModel.getIndustryName());

        List<JobFairCompanyPostModel> jobFairCompanyPostList = jobFairCompanyPostService.getListBySidAndCid(sId, cId);
        List<JobPublishModel> jobPublishList = new ArrayList<>();
        if (jobFairCompanyPostList != null) {
            for (int i = 0; i < jobFairCompanyPostList.size(); i++) {
                JobPublishModel jobPublish = jobPublishService.findAllJobById(jobFairCompanyPostList.get(i).getPid());
                Integer expId = jobPublish.getExpId();
                Integer eduId = jobPublish.getEduId();
                if (expId != null) {
                    for (int j = 0; j < ExperienceTool.EXPERIENCE_VO_LIST.size(); j++) {
                        if (expId == ExperienceTool.EXPERIENCE_VO_LIST.get(j).getId()) {
                            if (expId == 1 || expId == 6) {
                                jobPublish.setExpRequireText(ExperienceTool.EXPERIENCE_VO_LIST.get(j).getData());
                            } else {
                                jobPublish.setExpRequireText(ExperienceTool.EXPERIENCE_VO_LIST.get(j).getData() + "年");
                            }

                        }
                    }
                }
                if (eduId != null) {
                    for (int j = 0; j < EducationTool.EDUCATION_VO_LIST.size(); j++) {
                        if (eduId == EducationTool.EDUCATION_VO_LIST.get(j).getId()) {
                            jobPublish.setMinEducationText(EducationTool.EDUCATION_VO_LIST.get(j).getData());
                        }
                    }
                }

                jobPublishList.add(jobPublish);
            }
        }
        enterprise.setJobPublishList(jobPublishList);
        LookMeModel lookMe2 = lookMeService.findRidAndPid(0, 0, enterprise.getId(), cus.getId(), 1, 2);//查询关注状态
        if (lookMe2 != null) {
            enterprise.setFollow(1);
        } else {
            enterprise.setFollow(0);
        }

        return Result.success(enterprise);

    }

    //触摸屏查询参会企业详情
    @PostMapping("/selectEnterpriseDetails")
    public Result<Object> selectEnterpriseDetails(
            @RequestParam("sId") Integer sId,
            @RequestParam("cId") Integer cId) {
        EnterpriseModel enterprise;
        enterprise = enterpriseService.findModelById(cId);
        if (enterprise == null) {
            return Result.success(null);
        }
        if (enterprise.getWelfare() != null && enterprise.getWelfare() != "") {
            String[] str = enterprise.getWelfare().split(",");
            List<String> welfareList = new ArrayList<>();
            for (int j = 0; j < str.length; j++) {
                welfareList.add(str[j]);
            }
            enterprise.setWelfareList(welfareList);
        } else {
            enterprise.setWelfareList(null);
        }
        List<EnterpriseAlbumModel> enterpriseAlbumModel = enterpriseAlbumService.getListByComId(enterprise.getId());
        enterprise.setAlbumModelList(enterpriseAlbumModel);
        for (int j = 0; j < ScaleTool.SCALE_VO_LIST.size(); j++) {
            if (enterprise.getScaleId() == ScaleTool.SCALE_VO_LIST.get(j).getId()) {
                enterprise.setScaleName(ScaleTool.SCALE_VO_LIST.get(j).getData());
            }
        }
        IndustryModel industryModel = industryService.getModelByCode(enterprise.getEnterpriseType());
        enterprise.setEnterpriseTypeText(industryModel.getIndustryName());

        List<JobFairCompanyPostModel> jobFairCompanyPostList = jobFairCompanyPostService.getListBySidAndCid(sId, cId);
        List<JobPublishModel> jobPublishList = new ArrayList<>();
        if (jobFairCompanyPostList != null) {
            for (int i = 0; i < jobFairCompanyPostList.size(); i++) {
                JobPublishModel jobPublish = jobPublishService.findAllJobById(jobFairCompanyPostList.get(i).getPid());
                Integer expId = jobPublish.getExpId();
                Integer eduId = jobPublish.getEduId();
                if (expId != null) {
                    for (int j = 0; j < ExperienceTool.EXPERIENCE_VO_LIST.size(); j++) {
                        if (expId == ExperienceTool.EXPERIENCE_VO_LIST.get(j).getId()) {
                            if (expId == 1 || expId == 6) {
                                jobPublish.setExpRequireText(ExperienceTool.EXPERIENCE_VO_LIST.get(j).getData());
                            } else {
                                jobPublish.setExpRequireText(ExperienceTool.EXPERIENCE_VO_LIST.get(j).getData() + "年");
                            }

                        }
                    }
                }
                if (eduId != null) {
                    for (int j = 0; j < EducationTool.EDUCATION_VO_LIST.size(); j++) {
                        if (eduId == EducationTool.EDUCATION_VO_LIST.get(j).getId()) {
                            jobPublish.setMinEducationText(EducationTool.EDUCATION_VO_LIST.get(j).getData());
                        }
                    }
                }

                jobPublishList.add(jobPublish);
            }
        }
        enterprise.setJobPublishList(jobPublishList);
        return Result.success(enterprise);

    }
}
